package com.ycd.Servlet;



import com.ycd.Entity.UserEntity;
import com.ycd.Entity.XinWenEntity;

import com.ycd.Service.XinWenEntityService;

import com.ycd.Service.impl.XinWenEntityServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description 添加新闻
 * @Author ycd
 * @Date : 2022/3/18 16:58
 * @Version : 1.0
 */
@WebServlet("/addNews")
public class AddNewsServlet extends HttpServlet {

    private Logger logger= LoggerFactory.getLogger(AddNewsServlet.class);
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //规范字符集
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        String newsTitile = req.getParameter("newsTitile");
        String newsText = req.getParameter("newsText");
        String LanMuid = req.getParameter("LanMuid");


        UserEntity user = (UserEntity)req.getSession().getAttribute("user");

        if (newsText!=null  &&LanMuid!=null && newsTitile!=null) {
            XinWenEntity xinWenEntity = new XinWenEntity();
            xinWenEntity.setCHNL_CODE(LanMuid);
            xinWenEntity.setTitle(newsTitile);
            xinWenEntity.setDETAIL(newsText);
            xinWenEntity.setOPTOR(user.getName());
            System.out.printf(newsTitile);

            logger.info("添加新闻信息{}",xinWenEntity.toString());
            XinWenEntityService xinWenEntityService=new XinWenEntityServiceImpl();
            xinWenEntityService.addXinWen(xinWenEntity);

            resp.sendRedirect("NewsManagement.html");

        }else {
            resp.sendRedirect("AddNews.html");
        }

        }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
